# -*- coding: UTF-8 -*-
# *******************************************************************
# File Name: generate
# > Author: 04000387
# > Created Time: 2025/1/20 13:34
# *******************************************************************
from pathlib import Path
import numpy as np


def read_file(_file_path, shuffle=False):
    text = []
    fp = open(_file_path, "r", encoding="utf-8")
    lines = fp.readlines()
    for line in lines:
        line = line.strip().replace(" ", "").replace("\t", "").replace("\n", "")
        if len(line) == 0:
            continue

        if shuffle:
            temp = [it for it in line]
            np.random.shuffle(temp)
            line = "".join(temp)

        text.append(line)

    fp.close()

    return text


def read_files(_file_paths, shuffle=False):
    texts = []
    for _file_path in _file_paths:
        texts.extend(read_file(_file_path, shuffle))
    return texts


def get_files(_root_dir, suffix=None, _num=-1):
    if suffix is None:
        suffix = [".png", ".jpg"]
    if isinstance(suffix, str):
        suffix = [suffix]

    _root_dir = Path(_root_dir)
    _root_dir.is_dir()
    if _root_dir.is_file():
        if _root_dir.suffix in suffix:
            return [_root_dir]
        return []

    _dirs = [_root_dir]

    _res = []

    while True:
        if len(_dirs) == 0:
            break

        _dir = _dirs.pop(0)
        for it in _dir.iterdir():
            if it.is_file() and it.suffix in suffix:
                _res.append(it)

            elif it.is_dir():
                _dirs.append(it)

            if 0 < _num <= len(_res):
                break

    return _res
